<?php
include 'test_header.php' ;

class test_class_problem_process extends UnitTestCase{
		
	private $db ;

	function setUp(){
		$this->db = db_connect();
		$sql = "
CREATE TABLE IF NOT EXISTS `problem` (
  `problem_no` bigint(20) NOT NULL DEFAULT '0',
  `title` varchar(256) DEFAULT NULL,
  `content` text,
  `input` varchar(1024) DEFAULT NULL,
  `output` varchar(2048) DEFAULT NULL,
  `sample_input` text,
  `sample_output` text,
  `tip` text,
  `source` varchar(1024) DEFAULT NULL,
  `time_limit` varchar(16) DEFAULT '1000',
  `mem_limit` varchar(16) DEFAULT '65536',
  `best_user` varchar(256) DEFAULT NULL,
  `submit_count` bigint(20) DEFAULT 0,
  `ac_count` bigint(20) DEFAULT 0,
  PRIMARY KEY (`problem_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
	
		$this->db->query( $sql );

	$sql = "
	INSERT INTO `problem` (`problem_no`, `title`, `content`, `input`, `output`, `sample_input`, 
	`sample_output`,`tip`, `source`, 
	`time_limit`, `mem_limit`, `best_user`) VALUES
	(110, 'a+b', 'a+b?', 'a and b', 'a+b=?', '123' , '123' ,'1+1=2', 'me', '123', '1024', 'me')
	";

		$this->db->query( $sql );
	}

	function tearDown(){
		$sql = "DROP TABLE `problem`" ;
		$this->db->query( $sql );
	}

	function test_get_new_problem_infomation_form_user_and_save_it(){
		
		$rand_no = rand();
		//模拟从用户得到的数据
		$request = array( 
		'problem_no' => $rand_no , 
		'title' =>'a+b' ,
		'content' => 'a+b?' , 
		'input' => 'a and b' ,
		'output' => 'a+b=?' ,
		'sample_input'=>'123',
		'sample_output'=>'123',
		'tip' => '1+1=2' , 
		'source' => 'me' , 
		'time_limit' => '123' ,
		'mem_limit' => '1024' ,
		'best_user' => 'me');

		$prob_p = new problem_process( $this->db , $request );
		$this->assertTrue( $prob_p->save() );

		$prob_p_1 = new problem_process( $this->db , $request );
		$this->assertTrue( $prob_p_1->load( 'problem_no' , $rand_no ) ) ;
		
		foreach( $request as $k => $v ){
			
			$this->assertEqual( $prob_p_1->get($k),$v );
		
		}	
	}

	function test_update_single_problem_infomation(){
		
		$prob_p = new problem_process( $this->db );
		$this->assertTrue( $prob_p->load( 'problem_no' , '110' ) ) ;
		$this->assertEqual( $prob_p->get( 'content') , 'a+b?' ) ;
		$this->assertTrue( $prob_p->set( 'content' , 'test') );
		$this->assertTrue( $prob_p->save() ) ;
		$this->assertTrue( $prob_p->load( 'problem_no' , '110' ) ) ;
		$this->assertEqual( $prob_p->get( 'content' ) , 'test' ) ;

	}

	function test_get_all_problem_infomation_from_databse_by_one_array(){

		$problems = problem_process::get_all($this->db);

		$this->assertTrue( !empty($problems) ) ;		
	}

	function test_when_user_submit_code_the_submit_count_will_plus(){

		$prob_p = new problem_process( $this->db );
		$this->assertTrue( $prob_p->load( 'problem_no' , '110' ) );
		$old_submit_count = $prob_p->get( 'submit_count' );
		$prob_p->plus_submit_count();
		$prob_p->save();
		$new_submit_count = $prob_p->get( 'submit_count' );
		$this->assertEqual( $new_submit_count , $old_submit_count+1 );
	}

	function test_when_user_ac_this_problem_th_ac_count_will_plus(){
		
		$prob_p = new problem_process( $this->db );
		$this->assertTrue( $prob_p->load( 'problem_no' , '110' ) );
		$old_ac_count = $prob_p->get( 'ac_count' );
		$prob_p->plus_ac_count();
		$prob_p->save();
		$new_ac_count = $prob_p->get( 'ac_count' );
		$this->assertEqual( $new_ac_count , $old_ac_count+1 );
	}
	
}
$test = new test_class_problem_process();
$test->run( new HtmlReporter() );



